package cn.easybuy.web.backend;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.easybuy.entity.Order;
import cn.easybuy.entity.OrderDetail;
import cn.easybuy.entity.Product;
import cn.easybuy.service.OrderService;
import cn.easybuy.service.ProductService;
import cn.easybuy.service.impl.OrderServiceImpl;
import cn.easybuy.service.impl.ProductServiceImpl;

public class OrderListServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		OrderService os=new OrderServiceImpl();
		ProductService ps=new ProductServiceImpl();
		PrintWriter out=response.getWriter();
		List<Order> orders=os.getOrdersList();
		StringBuffer ddlb=new StringBuffer("");
		for (Order order : orders) {
			ddlb.append("<li style=\"width: 950px;border: 1px solid #c7c7cc;text-align: center;margin:0 auto\"><table cellspacing=\"0\" >");
			ddlb.append("<tr bgcolor=\"#c7c7cc\">"
					+ "<td width=\"210px\">用户名:"+order.getLoginName()+"</td>"
					+ "<td width=\"280px\">订单号:"+order.getSerialNumber()+"</td>"
					+ "<td width=\"280px\">地址:"+order.getUserAddress()+"</td>"
					+ "<td width=\"180px\">￥"+order.getCost()+"</td></tr>"
					+ "<tr><table cellspacing=\"0\" border=\"1\" style=\"margin: 4px auto 20px\"><tr><td width=\"230px\">商品名称</td><td width=\"230px\">商品图片</td><td width=\"230px\">数量</td><td width=\"230px\">价格</td></tr>");
			List<Object> proList=ps.getObjectsByOrderId(order.getId());
			for (int i = 0; i < proList.size(); i++) {
				ddlb.append("<tr><td>"+((Product)proList.get(i)).getName()+"</td><td>"+((Product)proList.get(i)).getFileName()+"</td><td>"+((OrderDetail)proList.get(++i)).getQuantity()+"</td><td>"+((OrderDetail)proList.get(i)).getCost()+"</td></tr>");
			}
			ddlb.append("</table></tr></table></li>");
		}
		out.print(ddlb);
		out.flush();
		out.close();
		
		
	}

}
